
#import "DownloadTorrentListViewController.h"
#import "DownloadTorrentDetailsViewController.h"

@implementation DownloadTorrentListViewController

//
// get torrent list request
//
@synthesize getTorrentListRequest											= _getTorrentListRequest;

//
// server
//
@synthesize torrentClient													= _torrentClient;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup title, bar button
	//
	self.title																= NSLocalizedStringFromTable(@"Torrents", @"DownloadTorrentListViewController", nil);
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTorrentList:)] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{	
	self.getTorrentListRequest												= nil;
	self.torrentClient														= nil;
	
	[super dealloc];
}

//
// refresh
//
- (void)refreshTorrentList:(id)sender
{
	id getTorrentListCallback = ^void(id<GetTorrentDownloadListRequest> getTorrentDownloadListRequest)
	{
		[ActivityView removeView];
		NSError* error														= [getTorrentDownloadListRequest error];
		if(error)
		{
			NSString* title													= NSLocalizedStringFromTable(@"Get Torrent List Failed", @"DownloadTorrentListViewController", nil);
			[UIAlertView alertViewWithTitle:title message:error.localizedDescription cancel:NSLocalizedString(@"OK", nil)];
		}
		else
		{
			self.getTorrentListRequest										= getTorrentDownloadListRequest;
			[self.tableView reloadData];
		}
	};
	[ActivityView showActivityView:NSLocalizedStringFromTable(@"Getting Torrent List", @"DownloadTorrentListViewController", nil) networking:YES];
	[self.torrentClient getTorrentListWithCallback:getTorrentListCallback];
}

//
// did load
//
- (void)viewDidLoad
{	
	[super viewDidLoad];
	[self refreshTorrentList:nil];
}

//
// 
//
- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self.tableView reloadData];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.getTorrentListRequest torrentList].count;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return [self.torrentClient serverInfo].serverName;
}

//
// get footer
//
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section
{
	if(!self.getTorrentListRequest)
		return nil;
	
	NSString* totalSize														= [NSString formatSizeRate:[self.getTorrentListRequest totalSize] isRate:NO];
	NSString* uploadRate													= [NSString formatSizeRate:[self.getTorrentListRequest uploadRate] isRate:YES];
	NSString* downloadRate													= [NSString formatSizeRate:[self.getTorrentListRequest downloadRate] isRate:YES];
	return [NSString stringWithFormat:NSLocalizedStringFromTable(@"Total: %@\nUpload Rate: %@\nDownload Rate: %@", @"DownloadTorrentListViewController", nil), totalSize, uploadRate, downloadRate];
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	DownloadTorrentListViewCell* theCell									= (DownloadTorrentListViewCell*)[tableView dequeueReusableCellWithIdentifier:@"d>"];
	if(!theCell)
		theCell																= [[[DownloadTorrentListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d>"] autorelease];
	
	TorrentDownloadItem* item												= [[self.getTorrentListRequest torrentList] objectAtIndex:indexPath.row];
	theCell.nameLabel.text													= item.torrentName;
	theCell.statusLabel.text												= item.status;
	NSString* downloadSize													= [NSString formatSizeRate:item.downloadedSize isRate:NO];
	NSString* uploadSize													= [NSString formatSizeRate:item.uploadedSize isRate:NO];
	NSString* totalSize														= [NSString formatSizeRate:item.size isRate:NO];
	theCell.doneSizeLabel.text												= [NSString stringWithFormat:@"%@ / %@", downloadSize, totalSize];
	theCell.donePercentLabel.text											= [NSString stringWithFormat:NSLocalizedStringFromTable(@"[Done: %.1f%%]", @"DownloadTorrentListViewController", nil), (float)(item.downloadedSize * 100.0 / item.size)];
	NSString* uploadRate													= [NSString formatSizeRate:item.uploadRate isRate:YES];
	NSString* downloadRate													= [NSString formatSizeRate:item.downloadRate isRate:YES];
	theCell.uploadRateLabel.text											= [NSString stringWithFormat:NSLocalizedStringFromTable(@"Up: %@", @"DownloadTorrentListViewController", nil), uploadRate];
	theCell.downloadRateLabel.text											= [NSString stringWithFormat:NSLocalizedStringFromTable(@"Down: %@", @"DownloadTorrentListViewController", nil), downloadRate];
	theCell.shareRatioLabel.text											= [NSString stringWithFormat:@"%@ [%.3f]", uploadSize, item.shareRatio / 1000.f];
	theCell.accessoryType													= UITableViewCellAccessoryDisclosureIndicator;
	return theCell;
}

//
// get height
//
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
	return [DownloadTorrentListViewCell rowHeight];
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	DownloadTorrentDetailsViewController* torrentDetailsViewController		= [[[DownloadTorrentDetailsViewController alloc] init] autorelease];
	torrentDetailsViewController.torrentClient								= self.torrentClient;
	torrentDetailsViewController.torrentInfo								= [[self.getTorrentListRequest torrentList] objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:torrentDetailsViewController animated:YES];
}

//
// can edit
//
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath
{
	return YES;
}

//
// remove torrent
//
- (void)removeTorrent:(TorrentDownloadItem*)torrentInfo eraseData:(BOOL)eraseData
{
	id removeTorrentCallback = ^void(id<TorrentClientRequest> removeTorrentRequest)
	{
		[ActivityView removeView];
		NSError* error														= [removeTorrentRequest error];
		if(error)
		{
			NSString* title													= NSLocalizedStringFromTable(@"Remove Torrent Failed", @"DownloadTorrentListViewController", nil);
			[UIAlertView alertViewWithTitle:title message:error.localizedDescription cancel:NSLocalizedString(@"OK", nil)];
		}
		else
		{
			[self refreshTorrentList:nil];
		}
	};
	[ActivityView showActivityView:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Removing %@", @"DownloadTorrentListViewController", nil), torrentInfo.torrentName] networking:YES];
	[self.torrentClient removeTorrent:torrentInfo eraseData:eraseData callback:removeTorrentCallback];
}

//
// edit
//
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
	if(editingStyle == UITableViewCellEditingStyleDelete)
	{
		TorrentDownloadItem* torrentInfo									= [[self.getTorrentListRequest torrentList] objectAtIndex:indexPath.row];
		
		id onCancel = ^void()
		{
			[self removeTorrent:torrentInfo eraseData:YES];
		};
		
		id onDismiss = ^void(NSInteger buttonIndex)
		{
			[self removeTorrent:torrentInfo eraseData:NO];
		};
		
		NSString* title														= NSLocalizedString(@"Confirm", nil);
		NSString* message													= NSLocalizedStringFromTable(@"Delete Torrent With Data?", @"DownloadTorrentListViewController", nil);
		NSString* yesTitle													= NSLocalizedString(@"Yes", nil);
		NSString* noTitle													= NSLocalizedString(@"No", nil);
		[UIAlertView alertViewWithTitle:title message:message cancel:yesTitle others:[NSArray arrayWithObject:noTitle] onDismiss:onDismiss onCancel:onCancel];
	}
}


@end